package com.eshipenet.shipnet.dp_decompressor

import scala.util.{Failure, Try}

object Decompressor {

  def decompress(packet: EdgeChannelDataPacket): Try[EdgeChannelDataPacketUncompressed] =
    debase64(packet) flatMap { de =>
      de.compressor match {
        case "NONE" => decompressNone(de)
        case "XZ"   => decompressXZ(de)
        case _ =>
          Failure(new Exception(s"unsupported compressor: ${packet.compressor}"))
      }
    }

  def debase64(packet: EdgeChannelDataPacket): Try[EdgeChannelDataPacketDebase64] = Try {
    val data = java.util.Base64.getDecoder.decode(packet.data)
    EdgeChannelDataPacketDebase64(packet.id, data, packet.compressor, packet.compressorParams, packet.aux)
  }

  def decompressNone(packet: EdgeChannelDataPacketDebase64): Try[EdgeChannelDataPacketUncompressed] =
    Try {
      EdgeChannelDataPacketUncompressed(packet.id, packet.data, packet.aux)
    }

  // TODO
  def decompressXZ(packet: EdgeChannelDataPacketDebase64): Try[EdgeChannelDataPacketUncompressed] =
    ???
}
